<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'oi_year,oi_cate_name';

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = ['warehouselist','datemt'];



    //导出日期
    public function datemt(){
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }

        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $storage_info_model = new \app\common\model\Storage();
        $sort    = array('oi_date' => 'DESC','c_sort'=>'DESC','c_id'=>'DESC');


        //企业下所有出入库分类信息
        $storage_list = $storage_info_model->getStorageInfoListByDate($where,$sort,$this->auth->es_id);
        $result = array("total" => count($storage_list), "rows" => $storage_list);

        return json($result);

    }

    /**
     * 库存总价值
     */
    public function warehouselist(){
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();


        $storage_info_model = new \app\common\model\Storage;

        $storage_list = $storage_info_model->getStorageInfoListGroupYear($where,$this->auth->es_id);
        $data          = array();
        $warehouse_money_total = 0;
        foreach($storage_list as $key => $val){
            $data[] = array(
                'cate_name'  => $val['oi_year']?$val['oi_year'].'年'.$val['oi_cate_name']:$val['oi_cate_name'],
                'specs' => $val['oi_specs'],
                'total' => $val['surplus_num'].''.$val['oi_unit'],
                'price_total' => '￥'.$val['price_total'],
                'unit_Price'     =>'￥'.round($val['price_total']/$val['surplus_bottle_num']),
                'oi_is_fear_query' => $val['oi_is_fear_query']
            );
        }
        $result = array("total" => count($data), "rows" => $data);

        return json($result);
    }

    /**
     * 查看
     */
    public function index()
    {

        $storage_info_model = new \app\common\model\Storage;
        $where['oi_enterprise_id']     = $this->auth->es_id;
        $where['oi_warehouse']         = 2;
        $where['oi_surplus_num']       = ['>',0];
        $warehouse_money_total = $storage_info_model->getWarehoustMoney($where);
        $statistics['warehouse_money_total'] = $warehouse_money_total;



        //查询清账信息
        $where           = [];
        $where['oi_enterprise_id']     = $this->auth->es_id;
        $where['oi_warehouse']         = 1;
        $out_money_total = $storage_info_model->getPriceTotal($where);
        $where           = [];
        $where['oi_enterprise_id']    = $this->auth->es_id;
        $where['oi_warehouse']        = 2;
        $in_money_total  = $storage_info_model->getPriceTotal($where);
        $account_record_model = new \app\admin\model\account\Record();
        $where           = [];
        $where['oar_type']         = 1;
        $where['oar_status']       = 1;
        $where['oar_es_id']        = $this->auth->es_id;
        $out_account_total = $account_record_model->getMoneyTotal($where);
        $where           = [];
        $where['oar_type']       = 1;
        $where['oar_status']     = 2;
        $where['oar_es_id']      = $this->auth->es_id;
        $in_account_total = $account_record_model->getMoneyTotal($where);
        $statistics['out_warehouse_money_total'] = $out_money_total - $out_account_total;
        $statistics['in_warehouse_money_total'] = $in_money_total - $in_account_total;


        $statistics['year_profit']     = $storage_info_model->getProfit('year',$this->auth->es_id);
        $statistics['today_profit']      = $storage_info_model->getProfit('today',$this->auth->es_id);
        $statistics['yesterday_profit']      = $storage_info_model->getProfit('yesterday',$this->auth->es_id);
        $statistics['lastmonth_profit']      = $storage_info_model->getProfit('lastmonth',$this->auth->es_id);
        $statistics['thismonth_profit']      = $storage_info_model->getProfit('thismonth',$this->auth->es_id);

        $this->view->assign('statistics', $statistics);


        return $this->view->fetch();
    }

}
